﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.User
{
    public class Message
    {
        /// <summary>
        /// Return list of message from user
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.User.Message> GetListOfMessageFromUser(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR003_ListUserMessageFrom", 0, objListOfParameters);

                List<LimpaGaragem.Model.User.Message> objListOfMessage = new List<LimpaGaragem.Model.User.Message>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.User.Message objMessage = new LimpaGaragem.Model.User.Message();
                    objMessage.IDUserFrom = IDUser;
                    objMessage.ID = Convert.ToInt32(objDataRow["ID_MESSAGE"]);
                    if(objDataRow["ID_MESSAGE_PARENT"] != DBNull.Value)
                        objMessage.ID = Convert.ToInt32(objDataRow["ID_MESSAGE_PARENT"]);
                    objMessage.IDUserTo = Convert.ToInt32(objDataRow["ID_USER_TO"]);
                    objMessage.Title = objDataRow["DS_TITLE"].ToString();
                    objMessage.Content = objDataRow["DS_CONTENT"].ToString();
                    objMessage.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfMessage.Add(objMessage);
                }
                return objListOfMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return list of message to user
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.User.Message> GetListOfMessageToUser(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR003_ListUserMessageTo", 0, objListOfParameters);

                List<LimpaGaragem.Model.User.Message> objListOfMessage = new List<LimpaGaragem.Model.User.Message>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.User.Message objMessage = new LimpaGaragem.Model.User.Message();
                    objMessage.IDUserTo = IDUser;
                    objMessage.ID = Convert.ToInt32(objDataRow["ID_MESSAGE"]);
                    if (objDataRow["ID_MESSAGE_PARENT"] != DBNull.Value)
                        objMessage.ID = Convert.ToInt32(objDataRow["ID_MESSAGE_PARENT"]);
                    objMessage.IDUserFrom = Convert.ToInt32(objDataRow["ID_USER_FROM"]);
                    objMessage.Title = objDataRow["DS_TITLE"].ToString();
                    objMessage.Content = objDataRow["DS_CONTENT"].ToString();
                    objMessage.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfMessage.Add(objMessage);
                }
                return objListOfMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new favorite
        /// </summary>
        /// <returns>ID FAVORITE</returns>
        public int InsertMessage(int? IDMessageParent,
                                 int IDUserFrom,
                                 int IDUserTo,
                                 string Title,
                                 string Content)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                if(IDMessageParent.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_MESSAGE_PARENT", IDMessageParent));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_MESSAGE_PARENT", DBNull.Value));

                objListOfParameters.Add(new SqlParameter("@ID_USER_FROM", IDUserFrom));
                objListOfParameters.Add(new SqlParameter("@ID_USER_TO", IDUserTo));
                objListOfParameters.Add(new SqlParameter("@DS_TITLE", Title));
                objListOfParameters.Add(new SqlParameter("@DS_CONTENT", Content));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR003_InsertUserMessage", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete favorite
        /// </summary>
        public void DeleteMessage(int IDMessage)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_MESSAGE", IDMessage));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR003_DeleteUserMessage", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
